import type { MessageOption } from 'vue-devui/types/message'
import { Message } from 'vue-devui/message'
import type { FetchContext } from 'ofetch'
import type { FetchError } from '~/types/api'

export function dealWarning() {
  return useDebounce((res: Record<string, any>, logout: boolean = false) => {
    const msg = showMessage(res, logout)
    if (msg) {
      Message.warning(showMessage(res))
    }
  }, 200)
}
/**
 * 400010: 用户名密码错误
 * 400008: 登录被锁定
 * 400009: 登录惩罚升级
 * 400007: 验证码错误
 */
const failCode = ['400010', '400008', '400009', '400007']

export function showMessage(
  res: Record<string, any>,
  logout: boolean = false,
): string {
  if (logout) {
    emitEvent('logout', true)
    return ''
  }
  let message: string = ''
  const { status } = res
  const error_code = res.data.error_code?.toString() || ''
  switch (status) {
    case 400: // 参数错误直接返回错误信息
      if (!failCode.includes(error_code)) {
        message
          = error_code === '400013'
            ? '账号异常，请联系客服'
            : res.data.error_message
      }
      break
    case 401:
      // if (!failCode.includes(res.data.error_code.toString())) {
      //   message = res.data.error_message;
      // }
      emitEvent('logout', Boolean(localStorage.getItem('access_token')))
      break
    case 403:
      if (localStorage.getItem('access_token')) {
        emitEvent('forbiddenRefresh')
      }
      else {
        message = res.data.error_message
      }
      break
    case 404:
      break
    case 408:
      message = '请求超时'
      break
    case 451:// 审核中
      break
    case 500:
      message = '服务器错误'
      break
    case 501:
      message = '服务未实现'
      break
    case 502:
      message = '网络错误'
      break
    // case 503:
    //   location.href = '/503';
    //   break;
    case 504:
      message = '网络超时'
      break
    case 505:
      message = 'HTTP版本不受支持'
      break
    default:
      message = res?.data?.error_message || `连接出错`
  }
  return message
}

export const showErrorMessage = useDebounce((data: MessageOption) => {
  Message.warning(data)
}, 200)

/** userFetch response 处理 */
export function useFetchOnResponse(context: FetchContext) {
  const { /* request, */ response/* , options */ } = context
  const errorData: FetchError = response._data || {}
  /** 错误信息 */
  let errorMessage = errorData.error_message
  /** 是否拒绝弹框 */
  let eject = false
  switch (response.status) {
    case 400:
      if (!failCode.includes(`${errorData?.error_code}`)) {
        if (`${errorData?.error_code}` === '400013')
          errorMessage = '账号异常，请联系客服'
      }
      break
    case 401:
      emitEvent('logout', Boolean(localStorage.getItem('access_token')))
      eject = true
      break
    case 403:
      errorMessage = errorMessage || '权限不足'
      break
    case 404:// 404默认不弹出提示
      eject = true
      break
    case 451:// 审核中
      eject = true
      break
    case 502:
      errorMessage = '网络错误'
      break
    default:
      errorMessage = errorMessage || `连接出错`
      break
  }
  if (eject)
    return

  showErrorMessage({
    type: 'warning',
    message: errorMessage,
  })
}
